package com.mrx.pvzcommunity.controller;

import com.mrx.pvzcommunity.model.Response;
import com.mrx.pvzcommunity.model.Result;
import com.mrx.pvzcommunity.model.thread.Comment;
import com.mrx.pvzcommunity.model.thread.UserThread;
import com.mrx.pvzcommunity.model.user.AvatarUser;
import com.mrx.pvzcommunity.model.user.LogUser;
import com.mrx.pvzcommunity.model.user.NormalUser;
import com.mrx.pvzcommunity.model.user.User;
import com.mrx.pvzcommunity.service.CommentService;
import com.mrx.pvzcommunity.service.UserService;
import com.mrx.pvzcommunity.service.UserThreadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequiredArgsConstructor
@Api(tags = "用户操作")
@RequestMapping(value = "/user")
@CrossOrigin(originPatterns = "*", allowCredentials = "true")

public class UserController {

    private final UserService userService;

    private final UserThreadService userThreadService;

    private final CommentService commentService;

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @PostMapping("/login")
    @ApiOperation(value = "登录", notes = "登录 API")
    @ApiImplicitParam(name = "user", value = "要登陆的用户对象", paramType = "body", dataTypeClass = LogUser.class, required = true)

    public Result<String> login(@ApiIgnore HttpSession session, @RequestBody LogUser user) {
        return userService.isUserValid(session, user.toUser());
    }

    @PostMapping("/register")
    @ApiOperation(value = "注册", notes = "注册 API")
    @ApiImplicitParam(name = "user", value = "要注册的用户对象", paramType = "body", dataTypeClass = User.class, required = true)

    public Result<String> register(@ApiIgnore HttpSession session, @RequestBody User user) {
        if (userService.isUserNotExists(user)) {
            return userService.register(session, user);
        }
        return Result.createResult(Response.Code.REG_FAILURE_ACCOUNT_EXISTS);
    }

    @GetMapping("/avatar/{uid}")
    @ApiOperation(value = "获取头像", notes = "获取头像 API")
    @ApiImplicitParam(name = "uid", value = "用户 id", paramType = "path", dataTypeClass = Integer.class, required = true, example = "1")

    public Result<AvatarUser> avatar(@PathVariable String uid) {
        return userService.findUserByUid(uid);
    }

    @PostMapping("/forgot/{uid}")
    @ApiOperation(value = "找回密码", notes = "找回密码 API")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户 id", dataTypeClass = Integer.class, required = true, example = "1"),
            @ApiImplicitParam(name = "user", value = "密保答案", paramType = "body", required = true, dataTypeClass = String.class)
    })

    public Result<User> forgotPassword(@PathVariable String uid, @RequestBody String answer) {
        return userService.checkUidAndAnswer(uid, answer);
    }

    @GetMapping("/problem/{uid}")
    @ApiOperation(value = "密保问题", notes = "通过 uid 获取密保问题 API")
    @ApiImplicitParam(name = "uid", value = "用户 id", dataTypeClass = Integer.class, required = true, example = "1")

    public Result<String> getProblem(@PathVariable String uid) {
        return userService.getProblemByUId(uid);
    }

    @GetMapping("/logout")
    @ApiOperation(value = "登出", notes = "登出 API")

    public Result<String> logout(@ApiIgnore HttpSession session) {
        if (session.getAttribute("user") != null) {
            session.invalidate();
            return Result.createResult(Response.Code.SUCCESS);
        }
        return Result.createResult(Response.Code.USER_NOT_LOGGED);
    }

    @GetMapping("/threads")
    @ApiOperation(value = "我的帖子", notes = "查自己的帖子")

    public Result<List<UserThread>> getThreads(@ApiIgnore @SessionAttribute(value = "user", required = false) User user) {
        return userThreadService.findUserThreadByUser(user);
    }

    @GetMapping("/comments")
    @ApiOperation(value = "我的评论", notes = "查自己的评论")

    public Result<List<Comment>> getComments(@ApiIgnore @SessionAttribute(value = "user", required = false) User user) {
        return commentService.findCommentByUser(user);
    }

    @GetMapping("/friends")
    @ApiOperation(value = "关注列表", notes = "查询我的关注 API")

    public Result<List<NormalUser>> getFriends(@ApiIgnore @SessionAttribute(value = "user", required = false) User user) {
        return userService.getFriendsByUser(user);
    }

    @GetMapping("/whoami")
    @ApiOperation(value = "我是谁", notes = "获取当前用户信息 API")

    public Result<NormalUser> whoami(@ApiIgnore @SessionAttribute(value = "user", required = false) User user) {
        logger.info("user: {}", user);
        if (user == null) {
            return Result.createResult(Response.Code.USER_NOT_LOGGED, (NormalUser) null);
        }
        NormalUser normalUser = user.toNormalUser();
        logger.info("normalUser: {}", normalUser);
        return Result.createResult(Response.Code.SUCCESS, user.toNormalUser());
    }
}
